package com.webwork.mood.record.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webwork.mood.record.dao.RecordDao;
import com.webwork.mood.record.dao.RuleDao;
import com.webwork.mood.record.dataobject.MoodAverage;
import com.webwork.mood.record.dataobject.MoodRecord;
import com.webwork.mood.record.dataobject.MoodScore;
import com.webwork.mood.record.dataobject.Rule;
import com.webwork.mood.record.service.MoodService;
import com.webwork.mood.util.Paging;
import com.webwork.mood.util.StrManager;

@Service
public class MoodServiceImpl implements MoodService {

	@Autowired
	private RecordDao recordDao;
	
	@Autowired
	private RuleDao ruleDao;
	
	@Override
	public void insert(MoodRecord record) {
		recordDao.insert(record);
	}
	
	@Override
	public void update(MoodRecord record) {
		recordDao.update(record);
	}
	
	@Override
	public List<MoodRecord> query(MoodRecord record) {
		return this.query(record, new Paging(0, 0));
	}
	
	@Override
	public List<MoodRecord> query(MoodRecord record, Paging page) {
		return recordDao.query(record, page.getCurrPage(), page.getPageSize());
	}
	
	@Override
	public MoodRecord get(String recordId) {
		if (StrManager.isEmpty(recordId)) {
			return null;
		}
		MoodRecord queryObj = new MoodRecord();
		queryObj.setRecordId(recordId);
		List<MoodRecord> records = this.query(queryObj);
		if (records != null && records.size() > 0) {
			if (records.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return records.get(0);
			}
		} else {
			System.out.println("找不到该记录!");
		}
		return null;
	}

	@Override
	public int count(MoodRecord record) {
		return recordDao.count(record);
	}

	@Override
	public List<MoodScore> getMoodScoreTable(long startTime, long endTime) {
		Calendar now = Calendar.getInstance();
		Calendar before = (Calendar) now.clone();
		before.setTimeInMillis(startTime);
		Calendar after = (Calendar) now.clone();
		after.setTimeInMillis(endTime);
		List<MoodScore> ret = new ArrayList<MoodScore>();
		MoodScore ms = recordDao.getMoodScoreTable(before, after);
		ms.setTotalCount(ms.getTotalCount() == null ? 0 : ms.getTotalCount());
		ms.setAverageScore(ms.getAverageScore() == null ? 0 : ms.getAverageScore());
		ret.add(ms);
		return ret;
	}

	@Override
	public List<MoodAverage> getMoodScoreLine(long startTime, long endTime) {
		Calendar now = Calendar.getInstance();
		Calendar before = (Calendar) now.clone();
		before.setTimeInMillis(startTime);
		Calendar after = (Calendar) now.clone();
		after.setTimeInMillis(endTime);
		List<MoodAverage> mr0 = recordDao.getMoodScore(before, after, 0);
		List<MoodAverage> mr1 = recordDao.getMoodScore(before, after, 1);
		List<MoodAverage> ret = new ArrayList<MoodAverage>();
		
		if (mr0.size() > 0 && mr1.size() > 0) {
			Map<String, MoodAverage> map = new HashMap<String, MoodAverage>();
			for (MoodAverage ma : mr0) {
				MoodAverage average = new MoodAverage();
				average.setRecordDate(ma.getRecordDate());
				average.setNotManagerScore(ma.getTemp());
				average.setIsManagerScore(0f);
				map.put(ma.getRecordDate(), average);
			}
			for (MoodAverage ma : mr1) {
				if (map.get(ma.getRecordDate()) == null) {
					MoodAverage average = new MoodAverage();
					average.setRecordDate(ma.getRecordDate());
					average.setIsManagerScore(ma.getTemp());
					average.setNotManagerScore(0f);
					map.put(ma.getRecordDate(), average);
				} else {
					map.get(ma.getRecordDate()).setIsManagerScore(ma.getTemp());
				}
			}
			for (String date : map.keySet()) {
				ret.add(map.get(date));
			}
			Collections.sort(ret, new Comparator<MoodAverage>() {
				@Override
				public int compare(MoodAverage o1, MoodAverage o2) {
					return o1.getRecordDate().compareTo(o2.getRecordDate());
				}
			});
		} else if (mr0.size() > 0) {
			for (MoodAverage ma : mr0) {
				MoodAverage average = new MoodAverage();
				average.setRecordDate(ma.getRecordDate());
				average.setNotManagerScore(ma.getTemp());
				average.setIsManagerScore(0f);
				ret.add(average);
			}
		} else if (mr1.size() > 1) {
			for (MoodAverage ma : mr1) {
				MoodAverage average = new MoodAverage();
				average.setRecordDate(ma.getRecordDate());
				average.setIsManagerScore(ma.getTemp());
				average.setNotManagerScore(0f);
				ret.add(average);
			}
		}
		return ret;
	}
	
	@Override
	public String getRule() {
		Rule queryObj = new Rule();
		List<Rule> records = ruleDao.query(queryObj, 0, 0);
		if (records != null && records.size() > 0) {
			if (records.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return records.get(0).getValue();
			}
		} else {
			System.out.println("找不到该记录!");
		}
		return "";
	}

}
